<?php

include_once 'config.php';
/*
 * This file detals with database operations with users table
 */

class Users {

    private $dbName = 'users';
    //  private $connection = null;
    protected $id;
    protected $firstName;
    protected $lastName;
    protected $email;
    protected $userType;
    protected $role;
    protected $ban = false;
    protected $userArray = array();
    public $dbFieldNames = array('id', 'fname', 'lname', 'email', 'type', 'role', 'ban');

    function __construct() {
        // $this->connectToMySql = MySqlConnector::connectToMySql();
    }

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getFirstName() {
        return $this->firstName;
    }

    public function setFirstName($firstName) {
        $this->firstName = $firstName;
    }

    public function getLastName() {
        return $this->lastName;
    }

    public function setLastName($lastName) {
        $this->lastName = $lastName;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function getUserType() {
        return $this->userType;
    }

    public function setUserType($userType) {
        $this->userType = $userType;
    }

    public function getRole() {
        return $this->role;
    }

    public function setRole($role) {
        $this->role = $role;
    }

    public function getBan() {
        return $this->ban;
    }

    public function setBan($ban) {
        $this->ban = $ban;
    }

    /**
     * This method will return list of all users specified by user type.
     * @param type $userType
     * @return string
     */
    public static function getUsers($userType) {
        $userArray = array();
        $data_array = array();
        $query = mysql_query("select * from users where type = '$userType' ") or die(mysql_error());
        $i = 1;
        while ($row = mysql_fetch_array($query)) {
            $u = array();
            $u['id'] = $row['id'];
            $u['fname'] = $row['fname'];
            $u['lname'] = $row['lname'];
            $u['email'] = $row['email'];
            $u['ban'] = $row['ban'];
            $u['role'] = $row['role'];
            $u['type'] = $row['type'];
            $data_array[$i] = $u;
            $i++;
        }
        $userArray['user'] = $data_array;
        $userArray['status'] = 'SUCCESS';
        $userArray['total'] = $i;
        return $userArray;
    }

    /**
     * This method will create new user 
     * @param type $data = array of all info sent from user interface.
     */
    public static function createUser($data) {
        $u1 = $data;
//        echo '<pre>';
//        print_r($u1);
//        echo '</pre>';
//        echo '<br>' . $u1->firstName . '---' . $u1->lastName . '<br>';
//        $fieldIdNames = "(";
//        $counts = count($u1->dbFieldNames);
//        for ($index = 0; $index < $counts ; $index++) {
//            $fieldIdNames .= $u1->dbFieldNames[$index];
//            if($index+1<$counts){
//                $fieldIdNames .=",";
//            }
//        }
//        $fieldIdNames .=")";
        //echo $fieldIdNames;


        $result = mysql_query("insert into users (id,fname,lname,email,type,role,ban) values (NULL,'$u1->firstName','$u1->lastName','$u1->email','$u1->userType','$u1->role','$u1->ban' ) ") or die(mysql_error());
        //print_r($result);
        return $result;
    }

    public static function updateUser($data) {
        $u1 = $data;
        $uid = intval($u1->id);
        //echo 'update users set fname = "'.$u1->firstName.'", lname= "'.$u1->lastName.'", email = "'.$u1->email.'", role = "'.$u1->role.'", ban = "'.$u1->ban.'" where id= '.$uid;
        $result = mysql_query('update users set fname = "'.$u1->firstName.'", lname= "'.$u1->lastName.'", email = "'.$u1->email.'", role = "'.$u1->role.'", ban = "'.$u1->ban.'" where id= '.$uid) or die(mysql_error());
        return $result;
    }

    public static function deleteUser($data) {
        $u1 = $data;
        $uid = intval($u1->id);
        $result = mysql_query("delete from users where id = $uid ") or die(mysql_error());
        return $result;
    }

}
?>


